<?php
// 	session_start();
// 	echo "Ten cua ban la ".$_SESSION["name"];
//	echo $_POST["name1"];
	require_once 'ketnoi.php';
	date_default_timezone_set('Asia/Ho_Chi_Minh');
	
    $link = ketnoi();
    // $database="mydb";
    $database="ztruyen_gilf";
    mysqli_select_db ($link,$database) or die('toi khong the ket noi csdl');

	////////////////////////TEST DB///////////////////////////
	
	//////////////////////////////////////////////////////////
	
	date_default_timezone_set('Asia/Ho_Chi_Minh');
	
	//print_r($_POST);
	$proj_id = $_POST["uId"]; 
	$proj_email = $_POST["email"]; 
	$proj_first_name =  $_POST["first_name"];
	$proj_last_name = $_POST["last_name"];
	$proj_link  =  $_POST["link"];
	$proj_luckyNumber  =  $_POST["luckyNumber"];
	
	if ($_POST)
	{
    // Make a array with the values
	    $vals = array(
	        			'uId'     => $proj_id,
						'email'	  => $proj_email,
						'first_name'	  => $proj_first_name,
						'last_name'	  => $proj_last_name,
						'link'	  => $proj_link,
						'luckyNumber'	  => $proj_luckyNumber,
	    			);
	}
	if($proj_id == null)
		return;
   //////////////////////////////CHECK UID EXIT///////////////////////
   $bUidExits = 0;
   $bUidExitsCmd = "SELECT * FROM user WHERE UID = '{$proj_id}'";
   //echo $bUidExitsCmd;
   $bUidExits = mysqli_query($link,$bUidExitsCmd) or die("fail query 1");
   
   $resultUID = mysqli_fetch_array($bUidExits);
     
   if($resultUID != null)
   {
		echo "RESULT_EXITS";
		return;
   }
   $current_date = date('Y-m-d');
   $current_time = date('H:i:s');
   $cmdInsert = "INSERT INTO user (UID,LUCKYNUMBER,EMAIL,FIRST_NAME,LAST_NAME,LINK,DATE,TIME) VALUES('{$proj_id}','{$proj_luckyNumber}','{$proj_email}','{$proj_first_name}','{$proj_last_name}','{$proj_link}','{$current_date}','{$current_time}')";
   $resultInsert = mysqli_query($link,$cmdInsert);
   if($resultInsert)
   {
		echo "RESULT_INSERT_SUCCESS";
   }
   
   
   
   mysqli_close($link);
//     echo json_encode($vals);
?>